package org.example;

import org.example.excel.ExcelWriter;
import org.example.pojo.JavaType;
import org.example.pojo.Properties;
import org.example.read.ReadBom;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * XOM文件转为Excel文件
 */
public class XOMToExcel {
    public static void main(String[] args) {
        Map<String, JavaType> javaTypeMap = null;
        //1.NBUWXOM.bom
        String bomPath = "D:\\01MyFile\\赵政\\NBBOM\\bom\\NBUWXOM.bom";
        System.out.println("=================开始解析：NBUWXOM.bom=======================================");
        javaTypeMap = ReadBom.readBom(bomPath);
       /* for(Map.Entry<String,JavaType> entry:javaTypeMap.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }*/

        //2.解析NBUWXOM_zh_CN.voc
        String zh_CNPath = "D:\\01MyFile\\赵政\\NBBOM\\bom\\NBUWXOM_zh_CN.voc";
        File dirs = new File("D:\\01MyFile\\赵政\\bom转excel");
        if (!dirs.exists()) {
            System.out.println("创建目录：" + dirs.getName());
            dirs.mkdirs();
        }
        System.out.println("======================开始解析:NBUWXOM_zh_CN.voc=============================");
        List<JavaType> javaTypeList = ReadBom.readXomZhZc(zh_CNPath);
       /* for(JavaType ja:javaTypeList){
            System.out.println(ja);
        }*/
        System.out.println("======================开始合并======================");
        //3.合并
        for (int i = 0; i < javaTypeList.size(); i++) {
            JavaType javaType = javaTypeList.get(i);
            String className = javaType.getName();
            int index = className.lastIndexOf(".");
            String newClassName = className.substring(index + 1);
            if (javaTypeMap.containsKey(newClassName)) {
                System.out.println("找到BOM对象" + newClassName);
                JavaType newJavaType = javaTypeMap.get(newClassName);
                newJavaType.setDescription(javaType.getDescription());

                for (Properties properties : javaType.getPropertiesList()) {
                    if (newJavaType.getPropertiesMap().containsKey(properties.getName())) {
                        Properties newProperties = newJavaType.getPropertiesMap().get(properties.getName());
                        newProperties.setPhraseNavigation(properties.getPhraseNavigation());
                    }
                }
            }
        }
        for (Map.Entry<String, JavaType> entry : javaTypeMap.entrySet()) {
            System.out.println("合并后的BOM对象：" + entry.getKey());
            System.out.println("合并后的BOM属性" + entry.getValue());
        }
        //4.生成excel
        ExcelWriter.writeXomFile(javaTypeMap, dirs.getAbsolutePath());
    }
}
